package com.rp.user.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oracle.tools.packager.Log;
import com.rp.news.common.PagedResult;
import com.rp.news.enums.UserStatus;
import com.rp.news.util.CommonUtil;
import com.rp.news.util.extent.AliyunProperties;
import com.rp.pojo.AppUser;
import com.rp.user.mapper.AppUserMapper;
import com.rp.user.service.AppUserMngService;
import com.rp.vo.QueryUserListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AppUserMngServiceImpl implements AppUserMngService {

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private CommonUtil commonUtil;


    /**
     * 查询用户管理所有用户
     * @param vo
     * @return
     */
    @Override
    public PagedResult queryAll(QueryUserListVo vo) {

        Example example = new Example(AppUser.class);
        example.orderBy("createdTime").desc();
        Example.Criteria criteria = example.createCriteria();

        if(StrUtil.isNotBlank(vo.getNickname())){
            criteria.andLike("nickname","%" + vo.getNickname() +"%");
        }

        if(UserStatus.isUserStatusValid(vo.getStatus())){
            criteria.andEqualTo("activeStatus",vo.getStatus());
        }
        if(StrUtil.isNotBlank(vo.getStartDate())){
            criteria.andGreaterThanOrEqualTo("createdTime",DateUtil.parse(vo.getStartDate()));
        }
        if(StrUtil.isNotBlank(vo.getEndDate())){
            criteria.andLessThanOrEqualTo("createdTime",DateUtil.parse(vo.getEndDate()));
        }
        PageHelper.startPage(vo.getPage(),vo.getPageSize());
        List<AppUser> appUsers = appUserMapper.selectByExample(example);
        Log.info("==============");
        return commonUtil.setterPageResult(appUsers,vo.getPage());
    }

    @Override
    public void freezeUserOrNot(String userId, Integer doStatus) {
        AppUser appUser =new AppUser();
        appUser.setId(userId);
        appUser.setActiveStatus(doStatus);

        appUserMapper.updateByPrimaryKeySelective(appUser);
    }


}
